package org.example.com.cn.Business;

import org.example.com.cn.DAO.StudentDAO;
import org.example.com.cn.Model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ScoreService {
    StudentDAO studentDAO;


    @Autowired
    public void setStudentDAO(StudentDAO studentDAO) {
        this.studentDAO = studentDAO;
    }

    public float aveScore(){
        List<Student> students=studentDAO.queryAll();
        float ave=0;
        for(Student s:students){
            System.out.println(s.getSscore());
            ave+=s.getSscore();
        }
        return ave/students.size();
    }

    public void insertStudent(Student student) {
        studentDAO.insertStudent(student);
    }

    @Transactional
    public void modifyScore() {
        List<Student> students = studentDAO.queryAll();
        for(Student s : students) {
            int score = (int)s.getSscore();
            int Grade = score/10;
            switch (Grade) {
                case 10:
                case 9:s.setSscore(1f);System.out.println("A");break;
                case 8:s.setSscore(2f);System.out.println("B");break;
                case 7:s.setSscore(3f);System.out.println("C");break;
                case 6:s.setSscore(4f);System.out.println("D");break;
                default:s.setSscore(5f);System.out.println("E");
            }
            System.out.println(s.getSscore());
            studentDAO.modifyStudent(s);
        }
    }
}
